/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package unp.sisEventos.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import unp.sisEventos.pojos.Evento;
import unp.sisEventos.pojos.TipoEvento;
import unp.sisEventos.util.Utilitarios;
import unp.sisEventos.util.cls_json_data;

/**
 *
 * @author AngelAndres
 */
public class servlet_gestion_evento extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            int opt = 0;
            try {
                opt = Integer.parseInt(request.getParameter("opt"));
            } catch (Exception ex) {
            }
            String data_json = "";
            switch (opt) {
                case 1:
                    data_json = guardarEvento(request);
                    break;
                case 2:
                    data_json = obtenerEventos(request);
                    break;
                case 3:
                    data_json = deleteEvento(request);
                    break;
                case 4:
                    data_json = updateEvento(request);
                    break;
            }


            out.print(data_json);
        } finally {
            out.close();
        }
    }

    private String guardarEvento(HttpServletRequest request) {
        Evento evento = new Evento();
        TipoEvento tipoevento = new TipoEvento();
        SimpleDateFormat formatoDelTexto = new SimpleDateFormat("MM/dd/yyyy");
        tipoevento.setId(Integer.valueOf(request.getParameter("tipoevento_id")));
        evento.setNombre(request.getParameter("nombre"));
        evento.setDescripcion(request.getParameter("descripcion"));
        evento.setCapacidad(Integer.valueOf(request.getParameter("capacidad")));

        try {
            evento.setFecha(formatoDelTexto.parse(request.getParameter("fecha")));
        } catch (ParseException ex) {
            Logger.getLogger(servlet_gestion_evento.class.getName()).log(Level.SEVERE, null, ex);
        }
        evento.setLugar(request.getParameter("lugar"));
        evento.setModalidad(request.getParameter("modalidad").charAt(0));
        evento.setTipoevento(tipoevento);
        evento.setPrecio(Float.valueOf(request.getParameter("precio")));
        servlet_facade_web.getFacade().insertarEvento(evento);
        return Utilitarios.generarContextoJsonDesdeObject(new cls_json_data(""));
    }

    private String obtenerEventos(HttpServletRequest request) {
        ArrayList<Evento> eventos = new ArrayList<Evento>();
        eventos = servlet_facade_web.getFacade().getAllEvento();
        return Utilitarios.generarContextoJsonDesdeObject(new cls_json_data(eventos));
    }
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">

    /**
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

    private String deleteEvento(HttpServletRequest request) {
        HashMap<String, Integer> map = new HashMap<String, Integer>();
        map.put("id", Integer.valueOf(request.getParameter("id")));
        servlet_facade_web.getFacade().deleteEvento(map);
        return Utilitarios.generarContextoJsonDesdeObject(new cls_json_data(""));
    }

    private String updateEvento(HttpServletRequest request) {
        Evento evento = new Evento();
        TipoEvento tipoevento = new TipoEvento();
        SimpleDateFormat formatoDelTexto = new SimpleDateFormat("MM/dd/yyyy");
        tipoevento.setId(Integer.valueOf(request.getParameter("tipoevento_id")));
        evento.setNombre(request.getParameter("nombre"));
        evento.setDescripcion(request.getParameter("descripcion"));
        evento.setCapacidad(Integer.valueOf(request.getParameter("capacidad")));

        try {
            evento.setFecha(formatoDelTexto.parse(request.getParameter("fecha")));
        } catch (ParseException ex) {
            Logger.getLogger(servlet_gestion_evento.class.getName()).log(Level.SEVERE, null, ex);
        }
        evento.setLugar(request.getParameter("lugar"));
        evento.setModalidad(request.getParameter("modalidad").charAt(0));
        evento.setTipoevento(tipoevento);
        evento.setPrecio(Float.valueOf(request.getParameter("precio")));
        evento.setId(Integer.valueOf(request.getParameter("id")));
        servlet_facade_web.getFacade().updateEvento(evento);
        return Utilitarios.generarContextoJsonDesdeObject(new cls_json_data(""));
    }
}
